package com.zhongwang.mms.module.system.role;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.common.ValidResult;
import com.zhongwang.mms.gen.entity.Role;
import com.zhongwang.mms.module.system.log.LogService;
import com.zhongwang.mms.module.system.permission.PermissionModel;
import com.zhongwang.mms.module.system.permission.PermissionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 角色 控制层
 *
 * @Author: China.sgl
 * @Date: 2019/9/16 09:11
 */
@Controller
@RequestMapping("/sys/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private LogService logService;

    /**
     * 系统角色页面
     * @return 页面路径
     */
    @GetMapping
    @RequiresPermissions({"system:role:view"})
    public String page() {
        return "system/role/page";
    }

    /**
     * 系统角色数据
     * @return 系统帐户 Json 数据
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"system:role:view"})
    public List<RoleModel> list(Role role) {
        return roleService.findAll(role);
    }

    /**
     * 删除角色
     *
     * @param role 角色信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"system:role:edit"})
    public JsonResult del(Role role) {
        Role r = roleService.findRoleById(role.getId());
        logService.saveLog("删除角色" , role.getRoleName());
        return jsonResult(roleService.delRole(role));
    }

    /**
     * 角色名唯一验证
     *
     * @param r 角色
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/checkUnique")
    @RequiresPermissions({"system:role:view"})
    public ValidResult checkUnique(Role r) {
        Role role = roleService.findRoleByName(r.getRoleName());
        if (r.getId() == null) {
            return ValidResult.result(role);
        }
        if (role == null || role.getId().equals(r.getId())) {
            return ValidResult.SUCCESS;
        }
        return ValidResult.FAIL;
    }

    /**
     * 创建角色
     *
     * @param role 角色信息
     * @param permissionIds 选中权限 id 数组
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"system:role:edit"})
    public JsonResult save(Role role, Integer[] permissionIds) {
        logService.saveLog("新建角色" , role.getRoleName());
        return jsonResult(roleService.saveRole(role, permissionIds));
    }

    /**
     * 修改角色与权限
     *
     * @param role 角色信息
     * @param permissionIds 选中权限 id 数组
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions({"system:role:edit"})
    public JsonResult update(Role role, Integer[] permissionIds) {
        Role r = roleService.findRoleById(role.getId());
        logService.saveLog("修改角色", r, role);
        return jsonResult(roleService.updateRole(role, permissionIds));
    }

    /**
     * 查询所有权限
     *
     * @return json 数据
     */
    @GetMapping("/permissions")
    @ResponseBody
    @RequiresPermissions({"system:role:view"})
    public List<PermissionModel> permissions() {
        return permissionService.findAll();
    }

    /**
     * 查询所有权限，标识角色拥有权限
     *
     * @param id 角色 id
     * @return json 数据
     */
    @GetMapping("/permissions/{id}")
    @ResponseBody
    @RequiresPermissions({"system:role:view"})
    public List<PermissionModel> permissions(@PathVariable Integer id) {
        return permissionService.findPermissionCheckedByRoleId(id);
    }

    /**
     * 查询角色
     *
     * @param id 角色id
     * @return 角色
     */
    @ResponseBody
    @PostMapping("/id")
    @RequiresPermissions({"system:role:view"})
    public Role id(Integer id) {
        return roleService.findRoleById(id);
    }


}
